<?php
namespace app\http\middleware;
use think\facade\Session;
use think\facade\Cookie;
class Log
{
    /**
     * 记录访问日志
     */
    public function handle($request, \Closure $next){
        $configs = config('log.');
        $request->_refreshes = false;
        if($configs['close']){
            $where = array();
            $where['ip'] = array('eq',$request->ip());
            $where['url'] = array('eq',$request->url());
            $where['visittime'] = array('gt',$request->time()-30);
            $c = model('Visitlog')->where($where)->count();//前30秒的访问次数
            if($c>4){
                $request->_refreshes = true;
            }else{
                $params = array();
                $params['visittime'] = $request->time();
                $params['subdomain'] = $request->subDomain();
                $params['ip'] = $request->ip();
                $params['url'] = $request->url();
                $params['fromurl'] = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'';
                $params['pathinfo'] = $request->pathinfo();
                $params['token'] = Session::get('tokenlize')?Session::get('tokenlize'):$request->token();
                $params['memberid'] = Cookie::get('uid')?Cookie::get('uid'):0;
                $modelvisitlog = \app\common\model\Visitlog::create($params);
            }            
        }
        return $next($request);
    }
}